from flask import abort
from flask import current_app, jsonify
from flask import g
from flask import request
from info import db
from info.models import News, Comment, CommentLike, User
from info.response_code import RET
from info.utils.commons import user_login_data
from . import news_blue
from flask import render_template


# 关注与取消关注
# 请求路径: /news/followed_user
# 请求方式: POST
# 请求参数:user_id,action
# 返回值: errno, errmsg
@news_blue.route('/followed_user', methods=['POST'])
@user_login_data
def followed_user():
    # 1.判断用户是否登陆
    if not g.user:
        return jsonify(errno=RET.DATAERR, errmsg="用户没登陆")

    # 2.获取参数
    user_id = request.json.get("user_id")
    action = request.json.get("action")

    # 3.校验参数(为空校验)
    if not all([user_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 4.参数类型校验
    if action not in ["follow", "unfollow"]:
        return jsonify(errno=RET.DATAERR, errmsg="参数类型不正确")

    # 5.根据作者编号取出作者对象,判断作者对象是否存在
    try:
        author = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="获取作者编号失败")

    if not author:
        return jsonify(errno=RET.DBERR,errmsg="作者不存在")

    # 6.根据参数类型决定关注/取关
    try:
        if action == "follow":
            # 判断用户是否关注过作者
            if not g.user in author.followers:
                author.followers.append(g.user)
        else:
            if g.user in author.followers:
                author.followers.remove(g.user)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="获取失败")

    # 7.返回响应
    return jsonify(errno=RET.OK, errmsg="成功")


# 点赞&取消点赞
# 请求路径:/news/comment_like
# 请求方式；post
# 请求参数:news_id, comment_id ,action, g.user
# 返回值: errno, errmsg
@news_blue.route('/comment_like', methods=['POST'])
@user_login_data
def comment_like():
    # 判断用户是否登陆
    if not g.user:
        return jsonify(errno=RET.NODATA, errmsg="用户未登陆")

    # 获取参数
    comment_id = request.json.get("comment_id")
    action = request.json.get("action")

    # 校验参数(为空校验)
    if not all([comment_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 判断操作类型
    if not action in ["add", "remove"]:
        return jsonify(errno=RET.DATAERR, errmsg="操作类型有误")

    # 根据评论编号取出评论对象,判断对象是否存在
    try:
        comment = Comment.query.get(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="评论不存在")

    if not comment:
        return jsonify(errno=RET.DBERR, errmsg="获取评论失败")

    # 点赞或者取消点赞
    try:
        if action == "add":
            # 判断用户是否有点过赞
            comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id, CommentLike.user_id == g.user.id).first()
            if not comment_like:
                # 创建一个点赞对象
                comment_like = CommentLike()
                # 设置属性
                comment_like.comment_id = comment_id
                comment_like.user_id = g.user.id

                # 添加到数据库
                db.session.add(comment_like)
                db.session.commit()

                # 将评论的点赞数量加1
                comment.like_count += 1

        else:
            # 判断用户是否有点过赞
            comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id,
                                                CommentLike.user_id == g.user.id).first()
            if comment_like:
                # 删除一个点赞对象
                db.session.delete(comment_like)
                db.session.commit()

                # 将评论的点赞数量减1
                if comment.like_count > 0:
                    comment.like_count -= 1

    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="操作失败")

    # 返回响应
    return jsonify(errno=RET.OK, errmsg="成功")


# 收藏功能显示
# 请求路径:news/news_collect
# 请求方式:post
# 请求参数:news_id, action, g.user
# 返回值:errno, errmsg
@news_blue.route('/news_collect', methods=['POST'])
@user_login_data
def news_collect():
    # 判断用户是否登陆
    if not g.user:
        return jsonify(errno=RET.NODATA, errmsg="用户没登陆")

    # 获取参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")

    # 校验参数(为空校验)
    if not all([news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 判断操作类型
    if not action in ['collect', 'cancel_collect']:
        return jsonify(errno=RET.DBERR, errmsg="操作类型错误")

    # 根据新闻编号取出新闻对象,判断新闻对象是否存在
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="新闻获取失败")

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="新闻不存在")

    # 根据操作类型来收藏或者取消
    try:
        if action == "collect":
            # 判断用户是否已经收藏过新闻了
            if not news in g.user.collection_news:
                g.user.collection_news.append(news)
        else:
            # 用户已经收藏过新闻了
            if not news in g.user.collection_news:
                g.user.collection_news.remove(news)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="操作失败")

    # 返回响应
    return jsonify(errno=RET.OK, errmsg="成功")


# 请求路径: /news/news_comment
# 请求方式: post
# 请求参数:news_id, comment, parent_id, g.user
# 返回值: errno, errmsg, 评论字典
@news_blue.route('/news_comment', methods=['POST'])
@user_login_data
def news_comment():
    # 判断用户是否登陆
    if not g.user:
        return jsonify(errno=RET.NODATA, errmsg="用户未登陆")

    # 获取参数
    news_id = request.json.get("news_id")
    content = request.json.get("comment")
    parent_id = request.json.get("parent_id")

    # 校验参数(为空校验)
    if not all([news_id, content]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 根据新闻编号取出新闻,并判断新闻是否存在
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取新闻失败")

    if not news:
        return jsonify(errno=RET.DBERR, errmsg="新闻不存在")

    # 根据参数,创建评论对象,设置评论属性
    comment = Comment()
    comment.user_id = g.user.id
    comment.news_id = news_id
    comment.content = content

    # 判断是否有父评论
    if parent_id:
        comment.parent_id = parent_id

    # 添加评论到数据库
    try:
        db.session.add(comment)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="评论失败")

    # 返回响应
    return jsonify(errno=RET.OK, errmsg="成功", data=comment.to_dict())


# 请求路径: /news/<int:news_id>
# 请求方式: GET
# 请求参数:news_id
# 返回值: detail.html页面, 用户data字典数据
@news_blue.route('/<int:news_id>')
@user_login_data
def news_detail(news_id):

    # # 获取session中用户信息
    # user_id = session.get("user_id")
    #
    # # 查询用户对象
    # user = None
    # if user_id:
    #     try:
    #         user = User.query.get(user_id)
    #     except Exception as e:
    #         current_app.logger.error(e)

    # 根据新闻编号查询新闻对象
    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)

    # 判断新闻是否存在
    if not news:
        abort(404)

    # 查询前6条热门新闻
    try:
        click_news = News.query.order_by(News.clicks.desc()).limit(6).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取新闻失败")

    # 将新闻列表转换成字典列表
    click_news_list = []
    for item in click_news:
        click_news_list.append(item.to_dict())

    # 判断用户是否有收藏过该新闻
    is_collected = False
    if g.user and news in g.user.collection_news:
        is_collected = True

    # 查询所有的评论对象
    try:
        comments = Comment.query.filter(Comment.news_id == news_id).order_by(Comment.create_time.desc()).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取评论失败")

    # 获取用户点过的赞
    commentlikes = []
    try:
        if g.user:
            commentlikes = CommentLike.query.filter(Comment.user_id == g.user.id).all()
    except Exception as e:
        current_app.logger.error(e)

    # 获取点赞列表
    comment_like_ids = []
    for commentlike in commentlikes:
        comment_like_ids.append(commentlike.comment_id)

    # 评论列表转换成字典列表
    comment_list = []
    for comment in comments:

        comm_dict = comment.to_dict()
        # 设置点赞属性
        comm_dict["is_like"] = False

        # 判断用户是否对评论点过赞
        if g.user and comment.id in comment_like_ids:
            comm_dict["is_like"] = True
        comment_list.append(comm_dict)

    # 判断用户是否关注过该作者
    is_followed = False
    # 用户登陆且新闻有作者
    if g.user and news.user:
        # 用户在作者的粉丝列表里
        if g.user in news.user.followers:
            is_followed = True

    # 拼接数据渲染页面
    data = {
        "news_info": news.to_dict(),
        "click_news_list": click_news_list,
        "user_info": g.user.to_dict() if g.user else "",
        "is_collected": is_collected,
        "comments": comment_list,
        "is_followed": is_followed
    }

    return render_template("news/detail.html", data=data)